package com.qingcloud.base.remote.pojo;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.qingcloud.base.constant.SystemConstant;
import com.qingcloud.base.utils.AuthorityDeserializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

@Getter
@Setter
@Accessors(chain = true)
@ToString
public class LoginUserDTO implements UserDetails {

    @ApiModelProperty(value = "用户ID")
    private String userId;

    @ApiModelProperty(value = "登录名")
    private String username;

    @ApiModelProperty(value = "登录密码")
    private String password;

    @ApiModelProperty(value = "邮箱")
    private String workEmail;

    @ApiModelProperty(value = "姓名")
    private String fullName;

    @ApiModelProperty(value = "状态")
    private Integer status;

    @ApiModelProperty(value = "是否完全管理权限 0不是 1是")
    private Integer adminFlag;

    @ApiModelProperty(value = "是否企业管理员 0不是 1是")
    private Integer orgAdminFlag;

    @ApiModelProperty(value = "是否完全管理权限 0不是 1是")
    private Integer isAdmin;

    @ApiModelProperty(value = "登录结果 true成功 false失败")
    private Boolean loginSuccess;

    @ApiModelProperty(value = "登录失败原因")
    private String failMessage;

    @ApiModelProperty(value = "默认顶级监控对象")
    private String moId;

    @ApiModelProperty(value = "手机号码")
    private String mobileno;

    @ApiModelProperty(value = "用户类型")
    private Integer userType;

    @ApiModelProperty(value = "首页路由")
    private String indexRoute;

    @ApiModelProperty(value = "默认地区父级")
    private String parentDomainId;

    @ApiModelProperty(value = "默认地区ID")
    private String domainId;

    @ApiModelProperty(value = "默认地区名称")
    private String domainName;

    @ApiModelProperty(value = "默认地区类型")
    private String cityType;

    @ApiModelProperty(value = "是否网格员 1是 0不是")
    private Integer gridFlag;

    @ApiModelProperty(value = "是否网格长 1是 0不是")
    private Integer captainFlag;

    @ApiModelProperty(value = "是否有大气APP权限 1是 0不是")
    private Integer appFlag;

    @ApiModelProperty(value = "是否复审员 0不是 1是")
    private Integer reviewOfficerFlag;

    @ApiModelProperty(value = "是否审核员 0不是 1是")
    private Integer auditorFlag;

    @ApiModelProperty(value = "溯源地区市级编码")
    private Integer city;

    @ApiModelProperty(value = "溯源地区区县编码")
    private Integer district;

    @ApiModelProperty(value = "溯源地区街道编码")
    private Long street;

    @ApiModelProperty(value = "认证token")
    private String token;

    @ApiModelProperty(value = "当前登录的业务种类（大气，水，智慧环保等")
    private String businessType;

    private String openId;
    @ApiModelProperty(value = "用户组织")
    private String orgId;
    @ApiModelProperty(value = "企业名称")
    private String orgName;

    @ApiModelProperty(value = "用户头像")
    private String avatar;

    @ApiModelProperty(value = "用户角色")
    private List<String> roles;

    @Override
    @JsonDeserialize(using = AuthorityDeserializer.class)
    public Collection<SimpleGrantedAuthority> getAuthorities() {
        Collection<SimpleGrantedAuthority> collection = new HashSet<>();
        if (!CollectionUtils.isEmpty(this.getRoles())) {
            this.getRoles().forEach(role -> collection.add(new SimpleGrantedAuthority(role)));
        }
        return collection;
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        if (Integer.valueOf(SystemConstant.COMMON_ACCOUINT_LOCK).equals(this.status))
            return false;
        else
            return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        if (Integer.valueOf(SystemConstant.COMMON_ACCOUINT_FORBIDDEN).equals(this.status))
            return false;
        else
            return true;
    }
}
